home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 104_01 / runtime.asm < prev    next >
Assembly Source File  |  1980-01-01  |  12KB  |  886 lines

  1.     title    runtime package for small "C"
  2.  
  3. ram    equ    0        ;start of ram in system
  4.     extrn    main
  5.  
  6. ;********************************************************
  7. ;                            *
  8. ;    run time libray for small c compiler        *
  9. ;                            *
  10. ;        by Ron Cain                *
  11. ;                            *
  12. ;********************************************************
  13. ;
  14. ;    fetch a single byte from stack pointer plus
  15. ;    offset of byte following call and sign extend
  16. ;    into hl
  17. ;
  18. @gcharss:csect
  19.     pop    b    
  20.     ldax    b    
  21.     mov    l,a    
  22.     inx    b    
  23.     mvi    h,0    
  24.     dad    sp    
  25.     push    b    
  26.     mov    a,m    
  27.     jmp    @sxt
  28. ;
  29. ;    fetch a single byte from stack plus a 2 byte offset
  30. ;    and sign extend byte into hl
  31. ;
  32. @gcharsl:csect
  33.     pop    b
  34.     ldax    b
  35.     mov    l,a    
  36.     inx    b    
  37.     ldax    b    
  38.     mov    h,a    
  39.     inx    h    
  40.     dad    sp    
  41.     push    b    
  42.     mov    a,m    
  43.     jmp    @sxt    
  44. ;
  45. ;    fetch a single byte from address in hl and
  46. ;    sign extend into hl
  47. ;
  48. @gchar: csect
  49.     mov    a,m
  50.     jmp    @sxt
  51. ;
  52. ;    put the accum into hl and sign extend through h
  53. ;
  54. @sxt:    csect
  55.     mov    l,a
  56.     rlc
  57.     sbb    a
  58.     mov    h,a
  59.     ret
  60. ;
  61. ;    fetch a full 16-bit integer from offset plus stack pointer
  62. ;    into hl
  63. ;
  64. @gintss:csect
  65.     pop    b    
  66.     ldax    b    
  67.     mov    l,a    
  68.     mvi    h,0    
  69.     inx    b    
  70.     dad    sp    
  71.     push    b    
  72.     mov    a,m    
  73.     inx    h    
  74.     mov    h,m    
  75.     mov    l,a    
  76.     ret        
  77. ;
  78. ;    fetch a 16-bit integer from stack pointer plus
  79. ;    a 16 bit offset and put value in hl
  80. ;
  81. @gintsl:csect
  82.     pop    b
  83.     ldax    b
  84.     mov    l,a
  85.     inx    b
  86.     ldax    b
  87.     mov    h,a
  88.     inx    b
  89.     dad    sp
  90.     push    b
  91.     mov    a,m
  92.     inx    h
  93.     mov    h,m
  94.     mov    l,a
  95.     ret        
  96. ;
  97. ;    fetch a full 16-bit integer from the address in hl
  98. ;    into hl
  99. ;
  100. @gint:    csect
  101.     mov    a,m
  102.     inx    h
  103.     mov    h,m
  104.     mov    l,a
  105.     ret
  106. ;
  107. ;    store a byte stack plus 8 byte offset
  108. ;
  109. @pcharss:csect
  110.     xchg    
  111.     pop    b
  112.     ldax    b
  113.     mov    l,a
  114.     mvi    h,0
  115.     inx    b
  116.     dad    sp
  117.     mov    m,e
  118.     xchg
  119.     push    b
  120.     ret
  121. ;
  122. ;    store a byte at stack plus offset
  123. ;
  124. @pcharsl:csect
  125.     xchg
  126.     pop    b
  127.     ldax    b
  128.     mov    l,a
  129.     inx    b
  130.     ldax    b
  131.     mov    h,a
  132.     inx    b
  133.     dad    sp
  134.     mov    m,e        
  135.     xchg
  136.     push    b
  137.     ret
  138. ;
  139. ;    store 16-bit integer at offset plus stack 
  140. ;
  141. @pintss:csect
  142.     xchg
  143.     pop    b
  144.     ldax    b
  145.     mov    l,a
  146.     mvi    h,0
  147.     inx    b
  148.     dad    sp
  149.     mov    m,e
  150.     inx    h
  151.     mov    m,d
  152.     xchg
  153.     push    b
  154.     ret
  155. ;
  156. ;    store a 16 bit integer in hl at stack plus offset
  157. ;
  158. @pintsl:csect
  159.     xchg
  160.     pop    b
  161.     ldax    b
  162.     mov    l,a
  163.     inx    b
  164.     ldax    b
  165.     mov    h,a
  166.     inx    b
  167.     dad    sp
  168.     mov    m,e
  169.     inx    h
  170.     mov    m,d
  171.     push    b
  172.     xchg
  173.     ret
  174. ;
  175. ;    store a 16-bit integer in hl at the address in de
  176. ;
  177. @pint:    csect
  178.     mov    a,l
  179.     stax    d
  180.     inx    d
  181.     mov    a,h
  182.     stax    d
  183.     ret
  184.     page
  185. @incdec:csect
  186. ;
  187. ;    take the address in hl and add value that follow and save back
  188. ;    address in hl
  189. ;
  190. @preinc:
  191.     call    @incdecl
  192.     call    @inc
  193.     nop
  194.     ret    
  195. ;
  196. ;    take the address in hl and sub value that follow and
  197. ;    save it back in place
  198. ;
  199. @predec:
  200.     call    @incdecl
  201.     call    @dec
  202.     nop
  203.     ret
  204. ;
  205. ;    take the address in hl and sub value that follow and save
  206. ;    back in address in hl and restore value to pre inc
  207. ;
  208. @postinc:
  209.     call    @incdecl
  210.     push    d
  211.     call    @inc
  212.     pop    h
  213.     nop
  214.     ret
  215. ;
  216. ;    take the address in hl and add value that follow and
  217. ;    save back in address in hl restore value pre dec
  218. ;
  219. @postdec:
  220.     call    @incdecl
  221.     push    d
  222.     call    @dec
  223.     pop    h
  224.     nop
  225.     ret    
  226. ;
  227. ;    load needed valut for inc and dec 
  228. ;
  229. @incdecl:
  230.     pop    d
  231.     pop    b
  232.     ldax    b
  233.     inx    b
  234.     push    b
  235.     push    d
  236.     dcx    b
  237.     ora    a
  238.     jp    @incdec1
  239.     mov    e,m
  240.     inx    h
  241.     mov    d,m
  242.     ret
  243. @incdec1:
  244.     mov    e,m
  245.     mov    a,e
  246.     rlc
  247.     sbb    a
  248.     mov    d,a
  249.     ret
  250. ;
  251. ;    add value in a to de and save at address in hl
  252. ;
  253. @inc:
  254.     ani    07fh
  255.     add    e
  256.     mov    e,a
  257.     mov    a,d
  258.     aci    0
  259.     mov    d,a
  260.     jmp    @incdecs
  261. ;
  262. ;    subtract value in a to de and save at address in hl
  263. ;
  264. @dec:
  265.     ani    07fh
  266.     cma
  267.     inr    a
  268.     add    e
  269.     mov    e,a
  270.     mov    a,d
  271.     aci    0ffh
  272.     mov    d,a
  273.     jmp    @incdecs 
  274. ;
  275. ;    store value in de at address in hl
  276. ;
  277. @incdecs:
  278.     ldax    b
  279.     ora    a
  280.     jp    @incdec2
  281.     mov    m,d
  282.     dcx    h
  283.     mov    m,e
  284.     xchg
  285.     mov    a,h
  286.     ora    l
  287.     ret
  288. @incdec2:
  289.     mov    m,e
  290.     xchg
  291.     mov    a,h
  292.     ora    l
  293.     ret    
  294.     page
  295. ;
  296. ;    inclusive "or" hl and de into hl
  297. ;
  298. @or:    csect
  299.     pop    b
  300.     pop    d
  301.     push    b
  302.     mov    a,l
  303.     ora    e
  304.     mov    l,a
  305.     mov    a,h
  306.     ora    d
  307.     mov    h,a
  308.     ora    l
  309.     ret
  310. ;
  311. ;    excluseive "or" hl and de into hl
  312. ;
  313. @xor:    csect
  314.     pop    b
  315.     pop    d
  316.     push    b
  317.     mov    a,l
  318.     xra    e
  319.     mov    l,a
  320.     mov    a,h
  321.     xra    d
  322.     mov    h,a
  323.     ora    l
  324.     ret
  325. ;
  326. ;    "and" hl and de into hl
  327. ;
  328. @and:    csect
  329.     pop    b
  330.     pop    d
  331.     push    b
  332.     mov    a,l
  333.     ana    e
  334.     mov    l,a
  335.     mov    a,h
  336.     ana    d
  337.     mov    h,a
  338.     ora    l
  339.     ret
  340. ;
  341. ;    in all the following compare routines hl is set to 1 
  342. ;    if the condition is true, otherwise it is set to 0 
  343. ;
  344. ;    not current condition
  345. ;
  346. @nlog:    csect
  347.     mov    a,h
  348.     ora    l
  349.     jnz    @nlog1
  350.     lxi    h,1
  351.     mov    a,h
  352.     ora    l
  353.     ret
  354. @nlog1:
  355.     lxi    h,0
  356.     mov    a,h
  357.     ora    l
  358.     ret
  359.     page
  360. @comp:    csect
  361. ;
  362. ;    test if hl equal to de
  363. ;
  364. @eq:
  365.     pop    b
  366.     pop    d
  367.     push    b
  368.     lxi    b,@compret
  369.     push    b
  370.     call    @cmp
  371.     rz    
  372.     dcx    h
  373.     ret
  374. ;
  375. ;    test if de to hl for not equal
  376. ;
  377. @ne:
  378.     pop    b
  379.     pop    d
  380.     push    b
  381.     lxi    b,@compret
  382.     push    b
  383.     call    @cmp
  384.     rnz
  385.     dcx    h
  386.     ret
  387. ;
  388. ;    test if de greater then hl (signed)
  389. ;
  390. @gt:
  391.     pop    b
  392.     pop    d
  393.     push    b
  394.     lxi    b,@compret
  395.     push    b
  396.     xchg
  397.     call    @cmp
  398.     rc
  399.     dcx    h
  400.     ret
  401. ;
  402. ;    test if de less then hl (signed)
  403. ;
  404. @lt:
  405.     pop    b
  406.     pop    d
  407.     push    b
  408.     lxi    b,@compret
  409.     push    b
  410.     call    @cmp
  411.     rc
  412.     dcx    h
  413.     ret
  414. ;
  415. ;    test if de greater then or equal to hl (signed)
  416. ;
  417. @ge:
  418.     pop    b
  419.     pop    d
  420.     push    b
  421.     lxi    b,@compret
  422.     push    b
  423.     call    @cmp
  424.     rz
  425.     rnc
  426.     dcx    h
  427.     ret
  428. ;
  429. ;    test if de less than or equal to hl (signed)
  430. ;
  431. @le:
  432.     pop    b
  433.     pop    d
  434.     push    b
  435.     lxi    b,@compret
  436.     push    b
  437.     call    @cmp
  438.     rz
  439.     rc
  440.     dcx    h
  441.     ret
  442.     page
  443. ;
  444. ;    test if de greater than hl (unsigned)
  445. ;
  446. @ugt:
  447.     pop    b
  448.     pop    d
  449.     push    b
  450.     lxi    b,@compret
  451.     push    b
  452.     xchg
  453.     call    @ucmp
  454.     rc
  455.     dcx    h
  456.     ret
  457. ;
  458. ;    test if de less than hl(unsigned)
  459. ;
  460. @ult:
  461.     pop    b
  462.     pop    d
  463.     push    b
  464.     lxi    b,@compret
  465.     push    b
  466.     call    @ucmp
  467.     rc
  468.     dcx    h
  469.     ret
  470. ;
  471. ;    test if de greater than or equal to hl (unsigned)
  472. ;
  473. @uge:
  474.     pop    b
  475.     pop    d
  476.     push    b
  477.     lxi    b,@compret
  478.     push    b
  479.     call    @ucmp
  480.     rnc
  481.     dcx    h
  482.     ret
  483. ;
  484. ;    test if de less than or equal to hl (unsigned)
  485. ;
  486. @ule:
  487.     pop    b
  488.     pop    d
  489.     push    b
  490.     lxi    b,@compret
  491.     push    b
  492.     call    @ucmp
  493.     rz
  494.     rc
  495.     dcx    h
  496.     ret
  497. ;
  498. ;    common routine to preform a signed compare of
  499. ;    de and hl 
  500. ;
  501. ;    de-hl and sets the conditions:
  502. ;        carry set means de < hl
  503. ;        zero/non-zero set according to equality
  504. ;
  505. @cmp:
  506.     mov    a,e
  507.     sub    l
  508.     mov    e,a
  509.     mov    a,d
  510.     sbb    h
  511.     lxi    h,1
  512.     jm    @cmp1
  513.     ora    e
  514.     ret
  515. @cmp1:
  516.     ora    e
  517.     stc
  518.     ret
  519. ;
  520. ;    common routine to perform unsinged compare
  521. ;
  522. ;    carry set if de less than hl
  523. ;    zero/non-zero set accordingly hl=de
  524. ;
  525. @ucmp:
  526.     mov    a,d
  527.     cmp    h
  528.     jnz    @ucmp1
  529.     mov    a,e
  530.     cmp    l
  531. @ucmp1:
  532.     lxi    h,1
  533.     ret
  534. ;
  535. ;    set machine status for all comditional operators
  536. ;
  537. @compret:
  538.     mov    a,h
  539.     ora    l
  540.     ret
  541.     page
  542. ;
  543. ;    shift de arithmetically right by hl return in hl
  544. ;
  545. @asr:    csect
  546.     pop    b
  547.     pop    d
  548.     push    b
  549. @asr1:
  550.     xchg
  551.     mov    a,h
  552.     ral
  553.     mov    a,h
  554.     rar
  555.     mov    h,a
  556.     mov    a,l
  557.     rar
  558.     mov    l,a
  559.     dcr    e
  560.     jnz     @asr1
  561.     ret
  562. ;
  563. ;    shift de arithmetically left by hl and return in hl
  564. ;
  565. @asl:    csect
  566.     pop    b
  567.     pop    d
  568.     push    b
  569. @asl1:
  570.     xchg
  571.     dad    h
  572.     dcr    e
  573.     jnz    @asl1
  574.     ret
  575. ;
  576. ;    subtract hl from de and return in hl
  577. ;
  578. @sub:    csect
  579.     pop    b
  580.     pop    d
  581.     push    b
  582.     mov    a,e
  583.     sub    l
  584.     mov    l,a
  585.     mov    a,d
  586.     sbb    h
  587.     mov    h,a
  588.     ret
  589. ;
  590. ;    from the two's complement of hl
  591. ;    
  592. @neg:    csect
  593.     call    @com
  594.     inx    h
  595.     ret
  596. ;
  597. ;    from the one's complement of hl
  598. ;
  599. @com:    csect
  600.     mov    a,h
  601.     cma
  602.     mov    h,a
  603.     mov    a,l
  604.     cma
  605.     mov    l,a
  606.     ret
  607. ;
  608. ;    mutiply de by hl and return in hl (signed)
  609. ;
  610. @mult:  csect
  611.     pop    b
  612.     pop    d
  613.     push    b
  614.     mov    b,h
  615.     mov    c,l
  616.     lxi    h,0
  617. @mult1:
  618.     mov    a,c
  619.     rrc
  620.     jnc    @mult2
  621.     dad    d
  622. @mult2:
  623.     xra    a
  624.     mov    a,b
  625.     rar
  626.      mov    b,a
  627.     mov    a,c
  628.     rar
  629.     mov    c,a
  630.     ora    b
  631.     rz
  632.     xra    a
  633.     mov    a,e
  634.     ral
  635.     mov    e,a
  636.     mov    a,d
  637.     ral
  638.     mov    d,a
  639.     ora    e
  640.     rz
  641.     jmp    @mult1
  642. ;
  643. ;    divide de by hl and return quotient in hl
  644. ;     return remainder in de (signed)
  645. ;
  646. @div:    csect
  647.     pop    b
  648.     pop    d
  649.     push    b
  650.     mov    b,h
  651.     mov    c,l
  652.     mov    a,d
  653.     xra    b
  654.     push    psw
  655.     mov    a,d
  656.     ora    a
  657.     cm    @deneg
  658.     mov    a,b
  659.     ora    a
  660.     cm    @bcneg
  661.     mvi    a,16
  662.     push    psw
  663.     xchg
  664.     lxi    d,0
  665. @div1:
  666.     dad    h
  667.     call    @rdel
  668.     jz    @div2
  669.     call    @cmpbcde
  670.     jm    @div2
  671.     mov    a,l
  672.     ori    1
  673.     mov    l,a
  674.     mov    a,e
  675.     sub    c
  676.     mov    e,a
  677.     mov    a,d
  678.     sbb    b
  679.     mov    d,a
  680. @div2:
  681.     pop    psw
  682.     dcr    a
  683.     jz    @div3
  684.     push    psw
  685.     jmp    @div1
  686. @div3:
  687.     pop    psw
  688.     rp
  689.     call    @deneg
  690.     xchg
  691.     call    @deneg
  692.     xchg
  693.     ret
  694. ;
  695. ;    negate the integer in bc (used by divide only)
  696. ;
  697. @deneg:
  698.     mov    a,d
  699.     cma
  700.     mov    d,a
  701.     m